package org.pricejd.rcx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import josx.platform.rcx.Button;
import josx.platform.rcx.LCD;
import josx.platform.rcx.Motor;
import josx.platform.rcx.Sensor;
import josx.platform.rcx.Sound;
import josx.rcxcomm.RCXPort;



public class Brain {
	// This is so barebone, and it probably won't even work!
	// I want to start testing this tomorrow.  Not even
	// sure if it will work!
	public static void main(String args[]) throws InterruptedException {
		Motor.A.setPower(5);
		Motor.C.setPower(5);
		
		LightNavigator ln = new LightNavigator(Motor.A, Motor.C, Sensor.S1, Sensor.S2);
		RCXPort port;
		DataInputStream in = null;
		DataOutputStream out = null;
		Move move = null;
		int z,x,y;
		
		try {
			port = new RCXPort();
			in = new DataInputStream(port.getInputStream());
			out = new DataOutputStream(port.getOutputStream());
			move = new Move(ln,out);
		} catch (IOException e) {
			System.exit(-1);
		}
		boolean done = false;
		
		while(!done) {
			try {
				z = in.readInt();
				x = in.readInt();
				y = in.readInt();
				if(z == 0) {
					LCD.showNumber(0);
					move.gotoPoint(x,y);
				}
				if(z == 1) {
					LCD.showNumber(x);
					move.travel(x);
				}
				if(z == 2) {
					LCD.showNumber(x);
					move.rotate(x);
				}
				if(z == 3) {
					done = true;
				}
				if(z == 4) {
					LCD.showNumber(4);
					move.reset();
				}
				if(!ln.getCanMove()) {
					done = true;
				}
			} catch (IOException e) {}
		}
		Sound.playTone(330,40);
		Sound.playTone(294,40);
		Sound.playTone(262,40);
		Sound.playTone(294,40);
		Sound.playTone(330,40);
		Sound.playTone(330,40);
		Sound.playTone(330,40);
		Button.RUN.waitForPressAndRelease();
	}
}
